package org.sas.example.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.sas.example.constant.RedisConstants;
import org.sas.example.system.model.Result;
import org.sas.example.system.model.response.CaptchaResult;
import org.sas.example.util.RedisCacheUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 登录控制器
 *
 * @author LiJY
 * @date 2024/07/11
 */
@RestController
@RequiredArgsConstructor
public class LoginController {

    private final RedisCacheUtils redisCacheUtils;

    /**
     * 获取验证码
     *
     * @return {@link Map}<{@link String},{@link Object}>
     */
    @GetMapping("/getCaptcha")
    public Result<CaptchaResult> getCaptcha() {
        // 定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 40, 4, 2);
        // 生成一个唯一id
        long id = IdWorker.getId();
        // 存入缓存中，5分钟后过期
        String key = RedisConstants.IMAGE_CAPTCHA_PREFIX_KEY + id;
        redisCacheUtils.setCacheObject(key ,captcha.getCode());
        redisCacheUtils.expire(key ,RedisConstants.DEFAULT_TIMEOUT_SECONDS);
        return Result.success("获取验证码成功.", new CaptchaResult(String.valueOf(id), captcha.getCode(), captcha.getImageBase64Data()));
    }

    /**
     * 模拟短信验证码登录
     *
     * @param phone   电话
     * @return {@link Map}<{@link String},{@link Object}>
     */
    @GetMapping("/getSmsCaptcha")
    public Result<String> getSmsCaptcha(String phone) {
        // 示例项目，固定1234
        String smsCaptcha = "1234";
        // 存入缓存中，5分钟后过期
        String key = RedisConstants.SMS_CAPTCHA_PREFIX_KEY + phone;
        redisCacheUtils.setCacheObject(key ,smsCaptcha);
        redisCacheUtils.expire(key ,RedisConstants.DEFAULT_TIMEOUT_SECONDS);
        return Result.success("获取短信验证码成功.", smsCaptcha);
    }

    /**
     * 注销
     *
     * @param session  会话
     * @param request  请求
     * @param response 响应
     */
    @GetMapping("logout")
    public void logout(HttpSession session, HttpServletRequest request, HttpServletResponse response){
        session.invalidate();
        final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null){
            new SecurityContextLogoutHandler().logout(request,response,auth);
            auth.setAuthenticated(false);
            SecurityContextHolder.clearContext();
            SecurityContextHolder.getContext().setAuthentication(null);
        }
        Cookie cookie = new Cookie("JSESSIONID",null);
        cookie.setPath(request.getContextPath() + "/");
        cookie.setDomain("domain");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}
